package com.health.fragment.admindoc;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.health.viewscontrols.MainActivity_Menu_AdminDocter;
import com.health.viewscontrols.NovaApplication;
import com.health.R;
import com.health.adapter.DocHealthAdapter;
import com.health.db.FDbManager;
import com.health.fragment.NovaFragment;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tools.SPUtil;
import com.health.view.spinner.NiceSpinner;
import com.health.widget.EditTextWithDate;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 健康管理界面(医生管理)
 * 
 * @author nova
 *
 */
public class AdminDocterHealthManage extends NovaFragment implements OnClickListener {

	private View docHealthView = null;
	private FDbManager fdbmanger = null;
	private ARCHIVE_BASEINFO userInfo = null;
	private EditText ed_idcard;// 身份证号
	private EditText ed_name;// 身份证号
	private NiceSpinner dateTypeSp;// 查询类别
	private EditTextWithDate ed_time_start;// 时间开始
	private EditTextWithDate ed_time_end;// 时间结束
	private Button btn_search;

	private TextView nameTv;// 姓名
	private TextView sexTv;// 性别
	private TextView idCardTv;// 身份证号
	private TextView phoneTv;// 联系电话
	private TextView zhtjsjTv;// 最后体检时间
	private TextView examDataTv;// 查看体检数据
	private TextView ysjyTv;// 医生建议
	private TextView isUpdataTv;// 是否上传
	private ListView listView;

	private DocHealthAdapter docHealthAdapter = null;
	String str_jdr = "";
	String str_jdrno = "";

	@Override
	public View findViews(LayoutInflater inflater) {
		fdbmanger = NovaApplication.fdbmanger;
		userInfo = NovaApplication.getUserInfo();
		docHealthView = inflater.inflate(R.layout.main_doc_healthmanage, null);
		initViews(docHealthView);
		return docHealthView;
	}

	@Override
	public void setupView(View view) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addListener() {
		// TODO Auto-generated method stub
		btn_search.setOnClickListener(this);

	}

	@Override
	public void setData() {
//		SharedPreferences sp = getActivity().getSharedPreferences(GlobalConts.SHAREPREFRENCE_NAME,
//				Activity.MODE_PRIVATE);
//		str_jdr = sp.getString("str_jdr", "");
//		str_jdrno = sp.getString("str_jdrno", "");
		str_jdr = SPUtil.getstr_jdr(getActivity());
		str_jdrno = SPUtil.getStr_jdrno(getActivity());
		String[] strs = { "无", "体检日期", "建档日期" };
		dateTypeSp.attachDataSource(Arrays.asList(strs));

		setListHead();
		List<ARCHIVE_BASEINFO> infos = fdbmanger.findAllByWhere(ARCHIVE_BASEINFO.class,
				" CREATER='" + str_jdr + "' AND CREATED_BY='" + str_jdrno + "'");
		docHealthAdapter = new DocHealthAdapter(getActivity(), infos);
		listView.setAdapter(docHealthAdapter);
	}

	/**
	 * 初始化布局
	 * 
	 * @author 王婷婷
	 * @date 2017年2月10日 下午3:17:44
	 */
	private void initViews(View v) {
		ed_idcard = (EditText) v.findViewById(R.id.ed_idcard);// 身份证号
		ed_name = (EditText) v.findViewById(R.id.ed_name);// 身份证号
		dateTypeSp = (NiceSpinner) v.findViewById(R.id.sp_type);// 查询类别
		ed_time_start = (EditTextWithDate) v.findViewById(R.id.ed_time_start);// 时间开始
		ed_time_end = (EditTextWithDate) v.findViewById(R.id.ed_time_end);// 时间结束
		btn_search = (Button) v.findViewById(R.id.btn_search);
		listView = (ListView) v.findViewById(R.id.tongji_list_lv);
		ed_time_start.setBefore(new Date());
	}

	private void setListHead() {
		View head = View.inflate(getActivity(), R.layout.dochealthmanage_list_head, null);

		nameTv = (TextView) head.findViewById(R.id.tongji_list_name);
		idCardTv = (TextView) head.findViewById(R.id.tongji_list_idcard);
		sexTv = (TextView) head.findViewById(R.id.tongji_list_sex);
		phoneTv = (TextView) head.findViewById(R.id.tongji_list_phone);
		zhtjsjTv = (TextView) head.findViewById(R.id.tongji_list_zhtjsj);
		examDataTv = (TextView) head.findViewById(R.id.tongji_list_examdate);
		ysjyTv = (TextView) head.findViewById(R.id.tongji_list_ysjy);
		isUpdataTv = (TextView) head.findViewById(R.id.tongji_list_isupdatadate);

		nameTv.setOnClickListener(this);
		sexTv.setOnClickListener(this);
		idCardTv.setOnClickListener(this);
		phoneTv.setOnClickListener(this);
		zhtjsjTv.setOnClickListener(this);
		examDataTv.setOnClickListener(this);
		ysjyTv.setOnClickListener(this);
		isUpdataTv.setOnClickListener(this);

		listView.addHeaderView(head);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_search:
			// 查询列表
			reQueryList();
			break;

		default:
			break;
		}
	}

	/**
	 * 查询返回结果
	 * 
	 * @author 王婷婷
	 * @date 2017年2月17日 上午10:43:24
	 */
	private void reQueryList() {
//		SharedPreferences sp = getActivity().getSharedPreferences(GlobalConts.SHAREPREFRENCE_NAME,
//				Activity.MODE_PRIVATE);
//		String str_jdr = sp.getString("str_jdr", "");
//		String str_jdrno = sp.getString("str_jdrno", "");
		String str_jdr = SPUtil.getstr_jdr(getActivity());
		String str_jdrno = SPUtil.getStr_jdrno(getActivity());
		String str_name = ed_name.getText().toString();
		String str_id = ed_idcard.getText().toString();
		Date dateL = ed_time_start.getDate();
		Date dateH = ed_time_end.getDate();
		String dateType = dateTypeSp.getText().toString();
		str_name = TextUtils.isEmpty(str_name) ? "%" : str_name;
		str_id = TextUtils.isEmpty(str_id) ? "%" : str_id;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		String strWhere = "IDCARD LIKE '%" + str_id + "%' and " + "CUSTOMERNAME LIKE '%" + str_name + "%'  ";
		String where2 = "";

		if (dateType.equals("体检日期") || dateType.equals("建档日期")) {
			if (dateL == null || dateH == null) {
				((MainActivity_Menu_AdminDocter) getActivity()).showTextDialog("请输入完整的时间范围");
				return;
			}
			if (dateType.equals("体检日期")) {
				where2 = "AND  LAST_UPDATE_DATE between '" + format.format(dateL) + "'   and '" + format.format(dateH)
						+ "'";
			} else if (dateType.equals("建档日期")) {
				where2 = "AND  CREATED_DATE between '" + format.format(dateL) + "'  and '" + format.format(dateH) + "'";

			}
		}

		strWhere = strWhere + where2 + " AND CREATER='" + str_jdr + "' AND CREATED_BY='" + str_jdrno + "'";
		System.out.println("=====================" + where2);
		Log.w("sql查询语句", strWhere);
		List<ARCHIVE_BASEINFO> list = fdbmanger.findAllByWhere(ARCHIVE_BASEINFO.class, strWhere);
		docHealthAdapter.setInfoList(list);
		docHealthAdapter.notifyDataSetChanged();
	}

	@Override
	public void setXueYang(String xueYang) {

	}
}
